/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-12-29 09:42:25
 * @LastEditors: turingchang
 * @LastEditTime: 2020-12-29 11:08:13
 */

'use strict';

const Service = require('egg').Service;
const schedule = require('node-schedule');
const { Op } = require("sequelize");
const fetch = require("node-fetch");

class TtbPushLogService extends Service {
    /**
     * 推送记录列表
     * @param {*} params 
     */
    async list(params){
        const ctx =this.ctx;
        let res = {
            success:false,
            msg:'',
            response_code_enum:null,
        }      
        let push_log_list ={};
        let where ={};  
        if(!params){
            res.response_code_enum = ctx.app.enums.response_code_enum.miss_required_params;
            return res;
        }
        if(params.id && ctx.app.utils.validator.isNum(params.id)){
            where.id =params.id;
        }
        if(params.push_id && ctx.app.utils.validator.isNum(params.push_id)){
            where.push_id =params.push_id;
        }
        if(params.success && ctx.app.utils.validator.isNum(params.success)){
            where.success =params.success;
        }
        if(params.uuid){
            where.uuid =params.uuid;
        }
        where.delete_time =null;
        push_log_list = await this.ctx.model.Push.TtbPushLog.findAndCountAll({
            attributes: { exclude: ['delete_time']},
            where:where,
            order: [
                ['create_time', 'desc']
            ],
            limit: ctx.limit,
            offset: ctx.offset,
            distinct: true,
            subQuery:false,
        });
        return push_log_list;
    }

    /**
     * 推送记录详情
     * @param {*} params 
     */
    async info(params){
        const ctx =this.ctx;
        let res = {
            success:false,
            msg:'',
            response_code_enum:null,
        }      
        let push_log ={};
        let where ={};  
        if(!params){
            res.response_code_enum = ctx.app.enums.response_code_enum.miss_required_params;
            return res;
        }
        if(params.id && ctx.app.utils.validator.isNum(params.id)){
            where.id =params.id;
        }
        if(params.uuid){
            where.uuid =params.uuid;
        }
        where.delete_time =null;
        push_log = await this.ctx.model.Push.TtbPushLog.findOne({
            attributes: { exclude: ['delete_time']},
            where:where,
        });
        return push_log;
    }

    /**
     * 推送记录详情
     * @param {*} params 
     */
    async add(params){
        const ctx =this.ctx;
        let res = {
            success:false,
            msg:'',
            response_code_enum:null,
        }      
        let push_log ={};
        if(!params){
            res.response_code_enum = ctx.app.enums.response_code_enum.miss_required_params;
            return res;
        }
        if(!params.push_id && !ctx.app.utils.validator.isNum(params.push_id)){
            res.response_code_enum = ctx.app.enums.response_code_enum.miss_required_params;
            return res;
        }
        // if(!params.result){
        //     res.response_code_enum = ctx.app.enums.response_code_enum.miss_required_params;
        //     return res;
        // }
        if(!params.success && !ctx.app.utils.validator.isNum(params.success)){
            push_log.success = push_log.success ==1 ? 1 :0;
        }
        push_log.uuid = ctx.app.utils.generator.uid();
        push_log.push_id = params.push_id;
        push_log.result = params.result || null;
        let save_result = await this.ctx.model.Push.TtbPushLog.create(push_log);
        if(!save_result){
            res.msg = "添加失败";
            res.response_code_enum = ctx.app.enums.response_code_enum.server_error;
            return res;
        }
        res.data = save_result;
        res.msg = "添加成功";
        return res;
    }



}

module.exports = TtbPushLogService;